load("@rules_jvm_external//:defs.bzl", "artifact")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//:java_grpc_library.bzl", "java_grpc_library")

java_library(
    name = "alts_internal",
    srcs = glob([
        "src/main/java/io/grpc/alts/internal/*.java",
    ]),
    deps = [
        ":handshaker_java_grpc",
        ":handshaker_java_proto",
        "//api",
        "//core:internal",
        "//grpclb",
        "//netty",
        "//stub",
        "@com_google_protobuf//:protobuf_java",
        "@com_google_protobuf//:protobuf_java_util",
        artifact("com.google.code.findbugs:jsr305"),
        artifact("com.google.guava:guava"),
        artifact("com.google.j2objc:j2objc-annotations"),
        artifact("io.netty:netty-buffer"),
        artifact("io.netty:netty-codec"),
        artifact("io.netty:netty-common"),
        artifact("io.netty:netty-handler"),
        artifact("io.netty:netty-transport"),
    ],
)

java_library(
    name = "alts",
    srcs = glob([
        "src/main/java/io/grpc/alts/*.java",
    ]),
    visibility = ["//visibility:public"],
    deps = [
        ":alts_internal",
        ":handshaker_java_grpc",
        ":handshaker_java_proto",
        "//api",
        "//auth",
        "//core:internal",
        "//netty",
        artifact("com.google.auth:google-auth-library-oauth2-http"),
        artifact("com.google.code.findbugs:jsr305"),
        artifact("com.google.guava:guava"),
        artifact("com.google.j2objc:j2objc-annotations"),
        artifact("io.netty:netty-common"),
        artifact("io.netty:netty-handler"),
        artifact("io.netty:netty-transport"),
    ],
)

# bazel only accepts proto import with absolute path.
genrule(
    name = "protobuf_imports",
    srcs = glob(["src/main/proto/grpc/gcp/*.proto"]),
    outs = [
        "protobuf_out/grpc/gcp/altscontext.proto",
        "protobuf_out/grpc/gcp/handshaker.proto",
        "protobuf_out/grpc/gcp/transport_security_common.proto",
    ],
    cmd = "for fname in $(SRCS); do " +
          "sed 's,import \",import \"alts/protobuf_out/,g' $$fname > " +
          "$(@D)/protobuf_out/grpc/gcp/$$(basename $$fname); done",
)

proto_library(
    name = "handshaker_proto",
    srcs = [
        "protobuf_out/grpc/gcp/altscontext.proto",
        "protobuf_out/grpc/gcp/handshaker.proto",
        "protobuf_out/grpc/gcp/transport_security_common.proto",
    ],
)

java_proto_library(
    name = "handshaker_java_proto",
    deps = [":handshaker_proto"],
)

java_grpc_library(
    name = "handshaker_java_grpc",
    srcs = [":handshaker_proto"],
    deps = [":handshaker_java_proto"],
)
